package com.wangjw.redis;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * Created by wangjw on 18/6/4.
 */
@Configuration
public class RedisConfig {

    @Value("${redis.host}")
    private String redisHost;

    @Value("${redis.port}")
    private Integer redisPort;

    @Value("${redis.password}")
    private String password;

    @Value("${redis.max.total}")
    private Integer maxTotal;

    @Value("${redis.max.idle}")
    private Integer maxIdle;

    @Value("${redis.min.idle}")
    private Integer minIdle;

    @Value("${redis.test.borrow}")
    private Boolean testOnBorrow;

    @Value("${redis.test.return}")
    private Boolean testOnReturn;

    @Bean(name = "redisPool")
    public JedisPool jedisPool() {
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(maxTotal);
        config.setMaxIdle(maxIdle);
        config.setMinIdle(minIdle);
        config.setTestOnBorrow(testOnBorrow);
        config.setTestOnReturn(testOnReturn);
        config.setBlockWhenExhausted(true); // 连接耗尽时是否阻塞，false抛出异常，true阻塞直到超时，默认为true
        return new JedisPool(config, redisHost, redisPort, 1000 * 2, password);
    }
}
